package com.magicborrow.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.HomepageActivity;
import com.magicborrow.activity.ICaLendActivity;
import com.magicborrow.activity.WantBorrowingInfoActivity;
import com.magicborrow.beans.WantBorringListBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.huanxin.ChatActivity;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.ViewPressedUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class WantBorrowingAdapter extends RefreshAdapter<WantBorringListBean.DataEntity.WantBorrowingBean> {

    private DisplayImageOptions options;

    public WantBorrowingAdapter(ArrayList<WantBorringListBean.DataEntity.WantBorrowingBean> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateItem(ViewGroup parent, int viewType) {
        return new WantBorrowingViewHolder(LayoutInflater.from(context).inflate(R.layout.want_borrowing_item, null));
    }

    @Override
    public void onBindItem(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof WantBorrowingViewHolder) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, WantBorrowingInfoActivity.class);
                    intent.putExtra("stuff", data.get(position));
                    context.startActivity(intent);
                }
            });

            WantBorrowingViewHolder viewHolder = (WantBorrowingViewHolder) holder;
            if (options == null)
                options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelOffset(R.dimen._30dp))).build();

            ImageLoader.getInstance().displayImage(Constants.ADDRESS + data.get(position).getUser().getAvatar80(),
                    viewHolder.imvHead, options);
            viewHolder.tvName.setText(data.get(position).getUser().getNickname());
            viewHolder.tvPublishTime.setText(data.get(position).getCreated());
            viewHolder.tvAddress.setText(data.get(position).getAddress());
            viewHolder.stuffName.setText(data.get(position).getName());
            viewHolder.tvTime.setText("想借" + data.get(position).getTimes() + PriceModeEnum.getModeStr(data.get(position).getTimesMode()));
            viewHolder.canBorrowingNumber.setText(data.get(position).getNumber() + "人可借");
            viewHolder.tvIGive.setOnTouchListener(ViewPressedUtil.TouchPress);
            viewHolder.tvInfo.setText(data.get(position).getDescription());
            viewHolder.tvIGive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!UserTools.isLogin(context,true))
                        return;
                    Intent intent = new Intent(context, ICaLendActivity.class);
                    intent.putExtra("borrowId", data.get(position).getId());
                    context.startActivity(intent);

                }
            });
            viewHolder.imvMsg.setOnTouchListener(ViewPressedUtil.TouchPress);
            viewHolder.imvMsg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!UserTools.isLogin(context,true))
                        return;
                    context.startActivity(new Intent(context, ChatActivity.class).putExtra("chatType", 1)
                            .putExtra("userId", data.get(position).getUser().getId() + "")
                            .putExtra("username", data.get(position).getUser().getNickname()).putExtra("avatarPath",
                                    data.get(position).getUser().getAvatar80()));
                }
            });

            //进入到他的主页
            ((WantBorrowingViewHolder) holder).imvHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, HomepageActivity.class);
                    intent.putExtra("id", data.get(position).getUser().getId());
                    context.startActivity(intent);
                }
            });
        }
    }

    public class WantBorrowingViewHolder extends RecyclerView.ViewHolder {
        private ImageView imvHead;
        private TextView tvName;
        private TextView tvPublishTime;
        private ImageView imvMsg;
        private TextView stuffName;
        private TextView tvTime;
        private TextView tvAddress;
        private TextView canBorrowingNumber;
        private TextView tvIGive;
        private TextView tvInfo;

        public WantBorrowingViewHolder(View view) {
            super(view);
            imvHead = (ImageView) view.findViewById(R.id.imv_head);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvPublishTime = (TextView) view.findViewById(R.id.tv_publish_time);
            imvMsg = (ImageView) view.findViewById(R.id.imv_msg);
            stuffName = (TextView) view.findViewById(R.id.stuff_name);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvAddress = (TextView) view.findViewById(R.id.tv_address);
            canBorrowingNumber = (TextView) view.findViewById(R.id.can_borrowing_number);
            tvIGive = (TextView) view.findViewById(R.id.tv_i_give);
            tvInfo = (TextView) view.findViewById(R.id.tv_info);
        }
    }
}
